﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;


public class GameProperty
{
    public float Origional{private set;get;}
    public float Max{private set;get;}
    public float Min{private set;get;}
    public float Active{private set;get;}
    

    public FloatEvent BeforValueChange;
    public FloatEvent AfterValueChange;

    
    public void Sub(float value)=>ChangeValue(value,"sub");

    public void Add(float value)=>ChangeValue(value,"add");

    public void Reset()
    {
        Active = Origional;
    }


    private void ChangeValue(float value,string behavior)
    {
        if(value < 0)
            Debug.LogError("用于游戏运算的值小于零!");


        BeforValueChange.Invoke(value);

        switch(behavior)
        {
            case "sub":
                Active -= value;
                if(Active>Max)
                    Active = Max;
            break;

            case "add":
                Active += value;
                if(Active<Min)
                    Active = Min;
            break;

            default:
            Debug.LogError("没有与之相匹配的GameProperty操作！操作符为："+behavior);
            break;
        }

        AfterValueChange.Invoke(Active);
    }

    private void InitEvent()
    {
        BeforValueChange = new FloatEvent();
        AfterValueChange = new FloatEvent();
    }

    public GameProperty(float origional,float max,float min)
    {
        this.Origional = origional;
        this.Max = max;
        this.Min = min;
        Active = origional;

        InitEvent();
    }

    public GameProperty(PropertiesDataGroup propertiesDataGroup)
    {
        this.Origional = propertiesDataGroup.Origional;
        this.Max = propertiesDataGroup.Max;
        this.Min = propertiesDataGroup.Min;
        Active = propertiesDataGroup.Origional;

        InitEvent();
    }
}


public struct PropertiesDataGroup
{
    public float Origional;
    public float Max;
    public float Min;
}

public class FloatEvent:UnityEvent<float>
{

}